package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.mapper.mwsreport.IMwsRemovalShipmentDetailMapper;
import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.models.mwsreportbean.MwsRemovalShipmentDetail;
import com.yunchang.springboot.models.mwsreportbean.MwsRemovalShipmentDetailDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:45
 * @description:
 */
@Component
public class RemovalShipmentDetailDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    @Autowired
    IMwsRemovalShipmentDetailMapper mwsRemovalShipmentDetailMapper;
    /**
     * 移除货件详情 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        int requestDateIndex = Integer.valueOf(String.valueOf(titleMap.get("request-date")));
//        String latestDay = mwsRemovalShipmentDetailMapper.getLatestDay(sellerId, area);
//        if (!StringUtil.isBlank(latestDay)) {
//            //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//            if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(lists.get(1).get(requestDateIndex))) >= 0) {
//                return null;
//            }
//        }
        int orderIdIndex = Integer.valueOf(String.valueOf(titleMap.get("order-id")));
        int shipmentDateIndex = Integer.valueOf(String.valueOf(titleMap.get("shipment-date")));
        int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
        int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
        int dispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("disposition")));
        int shippedQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("shipped-quantity")));
        int carrierIndex = Integer.valueOf(String.valueOf(titleMap.get("carrier")));
        int trackingNumberIndex = Integer.valueOf(String.valueOf(titleMap.get("tracking-number")));
        List<Object> insertList = new ArrayList<>(lists.size());
        for (int i = 1; i < lists.size(); i++) {
            MwsRemovalShipmentDetailDO mwsRemovalShipmentDetail = new MwsRemovalShipmentDetailDO();
            List<String> row = lists.get(i);
            mwsRemovalShipmentDetail.setSellerId(sellerId);
            mwsRemovalShipmentDetail.setArea(area);
            mwsRemovalShipmentDetail.setRequestDate(DateUtil.ISO8601ToGMT(row.get(requestDateIndex)));
            mwsRemovalShipmentDetail.setOrderId(row.get(orderIdIndex));
            mwsRemovalShipmentDetail.setShipmentDate(DateUtil.ISO8601ToGMT(row.get(shipmentDateIndex)));
            mwsRemovalShipmentDetail.setSku(row.get(skuIndex));
            mwsRemovalShipmentDetail.setFnsku(row.get(fnskuIndex));
            mwsRemovalShipmentDetail.setDisposition(row.get(dispositionIndex));
            mwsRemovalShipmentDetail.setShippedQuantity(ToolUtil.valueOfInteger(row.get(shippedQuantityIndex)));
            mwsRemovalShipmentDetail.setCarrier(row.get(carrierIndex));
            mwsRemovalShipmentDetail.setTrackingNumber(row.get(trackingNumberIndex));
            MwsRemovalShipmentDetailDO mwsRemovalShipmentDetailByParam = mwsRemovalShipmentDetailMapper.selectByMwsRemovalShipmentDetailAttribute(mwsRemovalShipmentDetail);
            if (null != mwsRemovalShipmentDetailByParam) {
                continue;
            }
            mwsRemovalShipmentDetail.setCreateTime(DateUtil.getDate());
            mwsRemovalShipmentDetail.setUpdateTime(DateUtil.getDate());
            mwsRemovalShipmentDetail.setFinanceStatus(1);
            insertList.add(mwsRemovalShipmentDetail);
        }
        return insertList;
    }
}
